import { deleteProps } from "./deleteProps.js";
const ONLY = 'only';
const EXCLUDE = 'exclude';
const INCLUDE = 'include';
/**
 * 重置对象属性
 * 适用前后端字段适配
 */
export function resetProps(data, keyMap, model = INCLUDE) {

    // 获取data keys
    let dataKeys = Object.keys(data);

    Object.keys(keyMap).forEach(oldKey => {
        let newKey = keyMap[oldKey];
        let originData = data[oldKey];
        data[newKey] = originData;

        // 移除旧属性
        if (model === EXCLUDE) {
            delete data[oldKey];
        }
    })

    // 移除多余属性
    if (model === ONLY) {
        deleteProps(data, dataKeys);
    }

    return data;
}